﻿#include "algo_arm_form.h"
#include "ui_algo_arm_form.h"
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include "singleton.h"
#include <QMessageBox>
bool Algo_Arm_Form::mode_setCorrectZone = false;
Algo_Arm_Form::Algo_Arm_Form(QString chnid, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Algo_Arm_Form)
{
    ui->setupUi(this);
    m_chnid = chnid;
    this->InputValidator();
    connect(ui->sb_setCorrectZone, &SwitchButton::checkedChanged, [this](bool checked)
    {
        this->mode_setCorrectZone = checked;
        if(checked)
            QMessageBox::information(nullptr, "提示", "已切换至尾绳检测矫正区域模式!");
        else
            QMessageBox::information(nullptr, "提示", "已切换至普通模式!");
    });
}

Algo_Arm_Form::~Algo_Arm_Form()
{
    delete ui;
}

void Algo_Arm_Form::InputValidator()
{
    QRegExp reg("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$");
    ui->led_ipcip->setValidator(new QRegExpValidator(reg));
    ui->led_thresh->setValidator(new QRegExpValidator(QRegExp("^0\\.[1-9]{1,2}$")));
    ui->led_ipcuser->setValidator(new QRegExpValidator(QRegExp("^[A-Za-z0-9]+$")));
    ui->led_ipcpsw->setValidator(new QRegExpValidator(QRegExp("^[A-Za-z0-9]+$")));
    ui->led_arm_maxpeople->setValidator(new QRegExpValidator(QRegExp("^[0-9]*$")));
}

void Algo_Arm_Form::on_btn_getparam_1_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("GetAlgoParam", body);
}

void Algo_Arm_Form::on_btn_setparam_1_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    body.insert("enable", int(ui->sb_enable->getChecked()));
    body.insert("thresh", ui->led_thresh->text().toFloat());
    body.insert("url", ui->led_url->text());
    QStringList m_type = this->type();
    body.insert("eventType",  m_type.at(0));
    body.insert("alarmType", m_type.at(1));
    body.insert("alarmGrade", ui->cbx_alarmgrade->currentIndex());
    body.insert("ipcIp", ui->led_ipcip->text());
    body.insert("ipcUser", ui->led_ipcuser->text());
    body.insert("ipcPassword", ui->led_ipcpsw->text());
    Singleton::Instance()->onSendParam("SetAlgoParam", body);
}

void  Algo_Arm_Form::onGetAlgoParam(QJsonObject obj)
{
    ui->sb_enable->setChecked(obj.value("enable").toInt());
    ui->led_thresh->setText(QString::number(obj.value("thresh").toDouble(), 'f', 10));
    ui->led_url->setText(obj.value("url").toString());
    QString eventType = obj.value("eventType").toString();
    if(eventType == "XJ_000")   ui->cbx_eventtype->setCurrentIndex(0);
    else if(eventType == "PD_000")   ui->cbx_eventtype->setCurrentIndex(1);
    else if(eventType == "ML_000")   ui->cbx_eventtype->setCurrentIndex(2);
    else if(eventType == "RQ_000")   ui->cbx_eventtype->setCurrentIndex(3);
    else if(eventType == "XW_000")   ui->cbx_eventtype->setCurrentIndex(4);
    else if(eventType == "ZC_000")   ui->cbx_eventtype->setCurrentIndex(5);
    else if(eventType == "JL_000")   ui->cbx_eventtype->setCurrentIndex(6);
    else if(eventType == "CY_000")   ui->cbx_eventtype->setCurrentIndex(7);
    else if(eventType == "HJ_000")   ui->cbx_eventtype->setCurrentIndex(8);
    else if(eventType == "SB_000")   ui->cbx_eventtype->setCurrentIndex(9);
    if(obj.value("alarmType").toString() != "")
    {
        ui->cbx_alarmtype->setCurrentIndex(obj.value("alarmType").toString().split("_").at(1).toInt() - 1);
    }
    ui->cbx_alarmgrade->setCurrentIndex(obj.value("alarmGrade").toInt());
    ui->led_ipcip->setText(obj.value("ipcIp").toString());
    ui->led_ipcuser->setText(obj.value("ipcUser").toString());
    ui->led_ipcpsw->setText(obj.value("ipcPassword").toString());
}

void Algo_Arm_Form::on_btn_getparam_2_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("GetAlgoPeopleCountParam", body);
}



void Algo_Arm_Form::on_btn_setparam_2_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    body.insert("maxPeople", ui->led_arm_maxpeople->text().toInt());
    body.insert("direct", ui->cbx_arm_direct->currentIndex());
    Singleton::Instance()->onSendParam("SetAlgoPeopleCountParam", body);
}


void Algo_Arm_Form::onGetAlgoPeopleCountParam(QJsonObject obj)
{
    ui->led_arm_maxpeople->setText(obj.value("maxPeople").toString());
    ui->cbx_arm_direct->setCurrentIndex(obj.value("direct").toInt());
}

void Algo_Arm_Form::onAlgoPeopleCountMaxChanged(QJsonObject obj)
{
    ui->led_arm_devId->setText(obj.value("devId").toString());
    ui->lcdNumber_max->display(obj.value("max").toInt());
    ui->lcdNumber_initial->display(obj.value("initial").toInt());
    ui->lcdNumber_current->display(obj.value("current").toInt());
    ui->lcdNumber_rest->display(obj.value("rest").toInt());
    if(obj.value("state").toInt())
    {
        ui->label_alarm->setPixmap(QPixmap(":/image/siren_red"));
    }
    else
    {
        ui->label_alarm->setPixmap(QPixmap(":/image/siren_green"));
    }
}

void Algo_Arm_Form::onGetAlgoPeopleCountMaxByTime(QJsonObject obj)
{
    ui->led_arm_devId->setText(obj.value("devId").toString());
    if(obj.value("timeAndMax").isArray())
    {
        QJsonArray arr = obj.value("timeAndMax").toArray();
        if(arr.count())
        {
            QJsonObject item = arr.at(0).toObject();
            ui->checkBox_set1->setChecked(true);
            ui->timeEdit_1->setTime(QTime(0, 0).addSecs(item.value("beginTime").toInt()));
            ui->timeEdit_d1->setTime(QTime(0, 0).addSecs(item.value("duration").toInt()));
            ui->lineEdit_count1->setText(QString::number(item.value("max").toInt()));
        }
        if(arr.count() > 1)
        {
            QJsonObject item = arr.at(1).toObject();
            ui->checkBox_set2->setChecked(true);
            ui->timeEdit_2->setTime(QTime(0, 0).addSecs(item.value("beginTime").toInt()));
            ui->timeEdit_d2->setTime(QTime(0, 0).addSecs(item.value("duration").toInt()));
            ui->lineEdit_count2->setText(QString::number(item.value("max").toInt()));
        }
        if(arr.count() > 2)
        {
            QJsonObject item = arr.at(2).toObject();
            ui->checkBox_set3->setChecked(true);
            ui->timeEdit_3->setTime(QTime(0, 0).addSecs(item.value("beginTime").toInt()));
            ui->timeEdit_d3->setTime(QTime(0, 0).addSecs(item.value("duration").toInt()));
            ui->lineEdit_count3->setText(QString::number(item.value("max").toInt()));
        }
    }
}



void Algo_Arm_Form::on_cbx_eventtype_currentIndexChanged(int eventtype)
{
    QStringList list_alarm ;
    switch (eventtype)
    {
        case 0:
            list_alarm << "巡检轨迹" << "人脸识别";
            break;
        case 1:
            list_alarm << "异物识别" << "锚杆识别" << "大块识别" << "水煤识别" << "皮带跑偏" << "堆煤检测";
            break;
        case 2:
            list_alarm << "皮带调速" << "煤量统计";
            break;
        case 3:
            list_alarm << "人员入侵" ;
            break;
        case 4:
            list_alarm << "安全帽检测" << "人员脱岗检测" << "吸烟检测" << "坐猴车" << "超挂车检测"
                       << "个人防护用品佩带" << "挂网检测" << "固定岗位工使用手机" << "乘车规范性检测"
                       << "设置遮拦检测" << "人员违规乘坐皮带" << "人员挂安全绳检测" << "信号灯识别";
            break;
        case 5:
            list_alarm << "钻杆计数" ;
            break;
        case 6:
            list_alarm << "距离监测报警" ;
            break;
        case 7:
            list_alarm << "划定区域内人员计数" << "公共区域内人数统计";
            break;
        case 8:
            list_alarm << "烟火识别" << "积水检测";
            break;
        case 9:
            list_alarm << "护帮支护检测" << "风门同时打开检测" << "人员进入自动开关风门"
                       << "阻车器复合检测" << "斜巷一坡三挡" << "单轨吊识别";
            break;
        default:
            break;
    }
    ui->cbx_alarmtype->clear();
    ui->cbx_alarmtype->addItems(list_alarm);
}

QStringList Algo_Arm_Form::type()
{
    QStringList type;
    QString eventtype;
    QString alarmtype;
    switch (ui->cbx_eventtype->currentIndex())
    {
        case 0:
            eventtype = "XJ_000";
            alarmtype = QString("XJ_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 1:
            eventtype = "PD_000";
            alarmtype = QString("PD_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 2:
            eventtype = "ML_000";
            alarmtype = QString("ML_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 3:
            eventtype = "RQ_000";
            alarmtype = QString("RQ_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 4:
            eventtype = "XW_000";
            alarmtype = QString("XW_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 5:
            eventtype = "ZC_000";
            alarmtype = QString("ZC_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 6:
            eventtype = "JL_000";
            alarmtype = QString("JL_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 7:
            eventtype = "CY_000";
            alarmtype = QString("CY_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 8:
            eventtype = "HJ_000";
            alarmtype = QString("HJ_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 9:
            eventtype = "SB_000";
            alarmtype = QString("SB_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        default:
            break;
    }
    type.append(eventtype);
    type.append(alarmtype);
    return  type;
}

void Algo_Arm_Form::on_btn_clear_people_count_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("ClearAlgoPeopleCount", body);
}

void Algo_Arm_Form::on_btn_GetPileCoalParam_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("GetPileCoalParam", body);
}

void Algo_Arm_Form::on_btn_SetPileCoalParam_clicked()
{
    emit sigSetPileCoalParam();
}



void Algo_Arm_Form::on_btn_SetAlgoPeopleCountMax_clicked()
{
    QJsonObject body;
    body.insert("devId", ui->led_arm_devId->text());
    body.insert("max", ui->led_arm_max->text().toInt());
    body.insert("initial", ui->led_arm_initial->text().toInt());
    Singleton::Instance()->onSendParam("SetAlgoPeopleCountMax", body);
}

void Algo_Arm_Form::on_btn_cleardevpeoplecount_clicked()
{
    QJsonObject body;
    body.insert("devId", ui->led_arm_devId->text());
    Singleton::Instance()->onSendParam("ClearDevAlgoPeopleCount", body);
}

void Algo_Arm_Form::on_btn_GetAlgoPeopleCountMaxByTime_clicked()
{
    QJsonObject body;
    body.insert("devId", ui->led_arm_devId->text());
    Singleton::Instance()->onSendParam("GetAlgoPeopleCountMaxByTime", body);
}

void Algo_Arm_Form::on_btn_SetAlgoPeopleCountMaxByTime_clicked()
{
    QJsonObject body;
    body.insert("devId", ui->led_arm_devId->text());
    QJsonArray arr;
    if(ui->checkBox_set1->isChecked())
    {
        QJsonObject obj;
        obj.insert("beginTime", QTime(0, 0).secsTo(ui->timeEdit_1->time()));
        obj.insert("duration", QTime(0, 0).secsTo(ui->timeEdit_d1->time()));
        obj.insert("max", ui->lineEdit_count1->text().toInt());
        arr.append(obj);
    }
    if(ui->checkBox_set2->isChecked())
    {
        QJsonObject obj;
        obj.insert("beginTime", QTime(0, 0).secsTo(ui->timeEdit_2->time()));
        obj.insert("duration", QTime(0, 0).secsTo(ui->timeEdit_d2->time()));
        obj.insert("max", ui->lineEdit_count2->text().toInt());
        arr.append(obj);
    }
    if(ui->checkBox_set3->isChecked())
    {
        QJsonObject obj;
        obj.insert("beginTime", QTime(0, 0).secsTo(ui->timeEdit_3->time()));
        obj.insert("duration",  QTime(0, 0).secsTo(ui->timeEdit_d3->time()));
        obj.insert("max", ui->lineEdit_count3->text().toInt());
        arr.append(obj);
    }
    body.insert("timeAndMax", arr);
    Singleton::Instance()->onSendParam("SetAlgoPeopleCountMaxByTime", body);
}

/************************************
 * @ Name   : algo_arm_form.cpp
 * @ Author : Wei.Liu
 * @ Created: 2022-05-04
 * @ brief  : device_arm add a save scene picture function
 ***********************************/

void Algo_Arm_Form::on_btn_SaveScenePicture_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("SaveScenePicture", body);
}


void Algo_Arm_Form::on_btn_scale_100_clicked()
{
    emit sigSetCompareZone(100);
}

void Algo_Arm_Form::on_btn_scale_150_clicked()
{
    emit sigSetCompareZone(150);
}

void Algo_Arm_Form::on_btn_scale_200_clicked()
{
    emit sigSetCompareZone(200);
}

void Algo_Arm_Form::on_btn_compare_cancel_clicked()
{
    emit sigSetCompareZone(0);
}
